﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using ArrayConsoleRender;

namespace CheckSort
{
    public class ArraySort
    {
        public static void ShellSort(Int32[] array)
        {
            Int32 j;
            Int32 step = array.Length / 2;
            while (step > 0)
            {
                for (Int32 i = 0; i < (array.Length - step); i++)
                {
                    j = i;
                    while ((j >= 0) && (array[j] > array[j + step]))
                    {
                        Int32 tmp = array[j];
                        array[j] = array[j + step];
                        array[j + step] = tmp;
                        j--;
                    }
                }
                step = step / 2;
            }

        }
        public static void ShellSortIllustraited(int[] array)
        {
            Int32 j;
            Int32 step = array.Length / 2;
            Int32 max = FindMax(array);
            ConsolePainter.PrepareScren();
            while (step > 0)
            {
                for (Int32 i = 0; i < (array.Length - step); i++)
                {
                    j = i;
                    while ((j >= 0) && (array[j] > array[j + step]))
                    {
                        Int32 tmp = array[j];
                        array[j] = array[j + step];
                        array[j + step] = tmp;
                        j--;
                    }
                    Console.Clear();
                    ConsolePainter.DrawArray(array, max);
                    Thread.Sleep(50);

                }
                step = step / 2;
            }
            ConsolePainter.DrawArray(array, max);
        }
        public static void BubleSort(Int32[] array)
        {
            Int32 lenght = array.Length;
            Int32 end = 0;
            for (Int32 i = 0; i < lenght; i++)
            {
                end = 0;
                for (Int32 j = 0; j < lenght - 1; j++)
                {
                    if (array[j] > array[j + 1])
                    {
                        Int32 tmp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = tmp;
                        end = 1;
                    }
                }
                if (end == 0) break;
            }

        }
        public static int FindMax(int[] arr)
        {
            int max = 0;
            int lenght = arr.Length;
            for (int i = 0; i < lenght; i++)
            {
                if (arr[i] > max) max = arr[i];
            }
            return max;
        }
        public static void BubleSortIllustraited(int[] array)
        {
            ConsolePainter.PrepareScren();
            Int32 lenght = array.Length;
            Int32 end = 0;
            Int32 max = FindMax(array);
            for (Int32 i = 0; i < lenght; i++)
            {
                end = 0;
                for (Int32 j = 0; j < lenght - 1; j++)
                {
                    if (array[j] > array[j + 1])
                    {
                        Int32 tmp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = tmp;
                        end = 1;
                        Console.Clear();
                        ConsolePainter.DrawArray(array, max);
                        Thread.Sleep(50);
                    }
                }
                if (end == 0) break;
            }

        }
    }
}
